package com.yx.ecglineview.customview;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.os.Build;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

/**
 * Created : Yx
 * Time : 2018/11/9 18:29
 * Describe :BrokenLineView折线图衍生品：折线图2
 */
public class BrokenLine2View extends View {
    private Context context;

    private Paint paint1, paint2;//画折线,低压，高压
    private Paint dashedPaint;//画虚线
    private Paint paintPoint1, paintPoint2;//画点,1低压电，2高压点
    private Paint paintText;//画文字

    //原点
    private int mXPoint = 100;//原点的X轴坐标
    private int mYPoint = 350;//原点的Y轴坐标
    //每隔多少dp画刻度
    private int mXScale = 45;//X轴的刻度长度
    private int mYScale = 50;//Y轴的刻度长度
    //刻度线长度
    public int mXLength = 300;//X轴的长度
    private int mYLength = 300;//Y轴的长度

    private int mRange[] = new int[]{60, 80, 100, 120, 140, 160};//画横线或虚线时，上面5dp处的数据
    private String[] mXLabel = new String[]{"07/11", "07/12", "7/13", "7/14", "7/15", "7/16", "7/17", "7/18", "7/19", "7/20"};//X轴上的刻度（周，日，月）
    //private String[] mYLabel = new String[]{"60", "80", "100", "120", "140", "160"};//Y轴上的刻度
    private String[] mData1 = new String[]{"60", "80", "100", "70", " 100", "100", "60", "80"};//低压的数据
    private String[] mData2 = new String[]{"120", "100", "100", "140", " 160", "120", "120", "100"};//高压的数据
    private String mTitle = "自定义折线图";

    private int mAColor = Color.parseColor("#087EFF");//低压线颜色
    private int mBColor = Color.parseColor("#FF6260");//高压线颜色
    private int mCColor = Color.parseColor("#F4F5F5");//横线颜色


    private float maxValue;//传入数据的最大值
    private int dataNum;//数据总数


    public BrokenLine2View(Context context) {
        super(context);
        this.context = context;
    }

    public BrokenLine2View(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public BrokenLine2View(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    @TargetApi(Build.VERSION_CODES.LOLLIPOP)
    public BrokenLine2View(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        this.context = context;
    }

    /**
     * 设置数据
     *
     * @param dataList
     */
    public void setDatas(String[] dataList) {
        //postInvalidate();//界面刷新
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        initPaint();//初始画笔
        drawAboutYs(canvas);//设置虚线
        initLine(canvas);//画X.Y坐标线及刻度
        inItBrokenLinOne(canvas, mData1);//折线图 数据
        inItBrokenLinTwo(canvas, mData2);
    }

    /**
     * 初始画笔
     */
    private void initPaint() {
        paint1 = new Paint();
        paint1.setStyle(Paint.Style.STROKE);//只绘制图形轮廓(描边)
        paint1.setStrokeWidth(2);//设置线宽
        paint1.setAntiAlias(true);//去锯齿
        paint1.setColor(mAColor);//蓝色
        paint1.setTextSize(12);

        paint2 = new Paint();
        paint2.setStyle(Paint.Style.STROKE);//只绘制图形轮廓(描边)
        paint2.setStrokeWidth(2);//设置线宽
        paint2.setAntiAlias(true);//去锯齿
        paint2.setColor(mBColor);//橙色
        paint2.setTextSize(12);

        // 横线/虚线
        dashedPaint = new Paint();
        dashedPaint.setAntiAlias(true);//抗锯齿
        dashedPaint.setFilterBitmap(true);//对位图进行滤波处理
        dashedPaint.setStyle(Paint.Style.STROKE);//只绘制图形轮廓(描边)
        dashedPaint.setStrokeWidth(1);//设置线宽
        dashedPaint.setColor(mCColor);//设置线宽

        //文字
        paintText = new Paint();
        paintText.setStyle(Paint.Style.STROKE);
        paintText.setAntiAlias(true);//去锯齿
        paintText.setColor(Color.DKGRAY);//深灰
        paintText.setTextSize(12);

        //点1
        paintPoint1 = new Paint();
        paintPoint1.setStyle(Paint.Style.STROKE);
        paintPoint1.setColor(mAColor);
        paintPoint1.setAntiAlias(true);//去锯齿
        paintPoint1.setStyle(Paint.Style.FILL);//实心

        //点2
        paintPoint2 = new Paint();
        paintPoint2.setStyle(Paint.Style.STROKE);
        paintPoint2.setColor(mBColor);
        paintPoint2.setAntiAlias(true);//去锯齿
        paintPoint2.setStyle(Paint.Style.FILL);//实心
    }

    /**
     * 根据点数，画横线
     *
     * @param canvas
     */
    private void drawAboutYs(Canvas canvas) {
        for (int i = 0; i < mRange.length; i++) {
            float ys = mYPoint - i * 50;
            Path path = new Path();
            // 画线
            path.reset();
            path.moveTo(getPaddingLeft(), ys);//起始点坐标
            path.lineTo(getWidth(), ys);//终点坐标
            canvas.drawPath(path, dashedPaint);
            canvas.drawText(mRange[i] + "", getPaddingLeft() + 20, ys - 5, paintText);
        }

    }

    /**
     * 画X.Y轴，及刻度
     *
     * @param canvas
     */
    private void initLine(Canvas canvas) {
        //根据数据的数量计算X轴的刻度长度
        calculateXScale();
        //设置X轴刻度
        for (int i = 0; i * mXScale < mXLength; i++) {
            //画刻度
            canvas.drawLine(mXPoint + i * mXScale, mYPoint, mXPoint + i * mXScale, mYPoint - 5, paintText);
            try {
                //画刻度值
                canvas.drawText(mXLabel[i], mXPoint + i * mXScale - 15, mYPoint + 20, paintText);
            } catch (Exception e) {
            }
        }


    }

    //根据数据的数量计算横坐标刻度
    private Integer calculateXScale() {
        mXLength = getWidth() - 200;
        Log.i("aaaa", "--width---" + mXLength + "------" + mXLength / mXLabel.length);
        mXScale = mXLength / (mXLabel.length - 1);
        return mXScale;
    }

    /**
     * 低压 折线1
     *
     * @param canvas
     */
    private void inItBrokenLinOne(Canvas canvas, String[] mData) {
        for (int i = 0; i < mData.length; i++) {
            float ys = mYPoint - (Float.parseFloat(mData[i]) - 60) * 2.5f;//当前折线点坐标
            //数据值(两点确定一条直线)
            if (i > 0) {  //大于0开始画线
                //画折线
                float yr = mYPoint - (Float.parseFloat(mData[i - 1]) - 60) * 2.5f;//上一折线点坐标
                canvas.drawLine(mXPoint + (i - 1) * mXScale, yr, mXPoint + i * mXScale, ys, paint1);
            }
            //画转折点上方的数据值
            paintText.setTextSize(12);
            canvas.drawText(mData[i], mXPoint + i * mXScale, ys - 10, paintText);
            //设置圆点为实心的
            //paint.setStyle(Paint.Style.FILL);
            //画两线之间的转折点
            canvas.drawCircle(mXPoint + i * mXScale, ys, 6, paintPoint1);
        }

        //设置标题的内容,位置,字体大小
        paintText.setTextSize(16);
        canvas.drawText(mTitle, 200, 20, paintText);
    }

    /**
     * 高压 折线2
     *
     * @param canvas
     */
    private void inItBrokenLinTwo(Canvas canvas, String[] mData) {
        for (int i = 0; i < mData.length; i++) {
            float ys = mYPoint - (Float.parseFloat(mData[i]) - 60) * 2.5f;//当前折线点坐标
            //数据值(两点确定一条直线)
            if (i > 0) {  //大于0开始画线
                //画折线
                float yr = mYPoint - (Float.parseFloat(mData[i - 1]) - 60) * 2.5f;//上一折线点坐标
                canvas.drawLine(mXPoint + (i - 1) * mXScale, yr, mXPoint + i * mXScale, ys, paint2);
            }
            //画转折点上方的数据值
            paintText.setTextSize(12);
            canvas.drawText(mData[i], mXPoint + i * mXScale, ys - 10, paintText);
            //设置圆点为实心的
            //paint.setStyle(Paint.Style.FILL);
            //画两线之间的转折点
            canvas.drawCircle(mXPoint + i * mXScale, ys, 6, paintPoint2);
        }
    }


}
